#include "FrameBufferHandler.h"

FrameBufferHandler::FrameBufferHandler(void)
{
}

FrameBufferHandler::~FrameBufferHandler(void)
{
}

void FrameBufferHandler::SetUpFBO( int width, int height, GLenum interFormat, GLenum format, GLuint type, GLenum target/*=GL_TEXTURE_2D*/ )
{
	framebuffer.width=width;
	framebuffer.height=height;
	framebuffer.interFormat=interFormat;
	framebuffer.format=format;
	framebuffer.target=target;
	framebuffer.type=type;

	buffer=(float*)malloc(width*height*3*sizeof(float));

	glewInit();

	glGenFramebuffersEXT(1,&(framebuffer.ID));

	InitTexBuffer();
}

void FrameBufferHandler::InitTexBuffer()
{
	glGenTextures(1,&(framebuffer.texID));
	glBindTexture(framebuffer.target,framebuffer.texID);
	glTexParameteri(framebuffer.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(framebuffer.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(framebuffer.target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(framebuffer.target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(framebuffer.target, 0, framebuffer.interFormat, framebuffer.width, framebuffer.height, 0, framebuffer.format, framebuffer.type, NULL);
}

void FrameBufferHandler::FBOActivate()
{
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,framebuffer.ID);

	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT,framebuffer.target,framebuffer.texID,NULL);	
}

void FrameBufferHandler::FBODeactivate()
{
	//glReadPixels(0, 0, framebuffer.width, framebuffer.height,GL_RGB,GL_FLOAT,buffer);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

